package com.pureut.storage.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.storage.dto.ImparityDto;
import com.pureut.storage.dto.PickingSaleDto;
import com.pureut.storage.dto.ReceiptAndPaymentDocDto;
import com.pureut.storage.dto.SaleInvoiceDto;
import com.pureut.storage.service.PickingSaleService;
import com.pureut.storage.vo.*;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.storage.api.vo.InvoiceVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 销售发货控制层
 *
 * @author: CM
 * @date: 2023/7/17  11:27
 */

@RestController
@RequestMapping("/pickingSale")
public class PickingSaleController extends BaseController {

    /**
     * 销售发货Service
     */
    @Resource
    PickingSaleService pickingSaleService;

    /**
     * 新增销售发货单
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:add")
    @Log(title = "新增销售发货", businessType = BusinessType.INSERT)
    @PostMapping("/addSale")
    public R<Void> addSale(@RequestBody PickingSaleVo pickingSaleVo) throws Exception {
        return toAjax(pickingSaleService.addSale(pickingSaleVo));
    }

    /**
     * 新增销售发货单(销售订单使用)
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:add")
    @Log(title = "新增销售发货", businessType = BusinessType.INSERT)
    @PostMapping("/addSaleOrder")
    public R<Void> addSaleOrder(@RequestBody PickingSaleVo pickingSaleVo) throws Exception {
        return toAjax(pickingSaleService.addSaleOrder(pickingSaleVo));
    }

    /**
     * 修改销售发货单
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:edit")
    @Log(title = "修改销售发货", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSale")
    public R<Void> updateSale(@RequestBody PickingSaleVo pickingSaleVo) throws Exception {
        return toAjax(pickingSaleService.updateSale(pickingSaleVo));
    }

    /**
     * 关结销售发货单据
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:close")
    @Log(title = "关结单据", businessType = BusinessType.UPDATE)
    @PostMapping("/closeSale")
    public R<Void> closeSale(@RequestBody PickingSaleVo pickingSaleVo) throws Exception {
        return toAjax(pickingSaleService.closeSale(pickingSaleVo));
    }


    /**
     * 删除单据
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteSale/{ids}")
    public R<Void> deleteSale(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingSaleService.deleteSale(ids));
    }

    /**
     * 提交审核
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:submit")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitSale/{ids}")
    public R<Void> submitSale(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingSaleService.submitSale(ids));
    }

    /**
     * 撤销审核
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:quash")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/revoke/{ids}")
    public R<Void> revoke(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingSaleService.revoke(ids));
    }


    /**
     * 查询销售发货列表
     */
    @GetMapping("/list")
    //@SaCheckPermission("warehouse:outbound:shipsale:list")
    public TableDataInfo<PickingSaleDto> getList(PickingSaleVo pickingSaleVo, PageQuery pageQuery) throws Exception {
        //查询销售发货列表
        return pickingSaleService.getList(pickingSaleVo, pageQuery);
    }

    /**
     * 审核单据
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:examine")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PutMapping("/saleReview")
    public R<Void> saleReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(pickingSaleService.saleReview(orderReviewVo));
    }

    /**
     * 查看单据
     */
    @GetMapping("/view/{id}")
    //@SaCheckPermission("warehouse:outbound:shipsale:query")
    public R<?> view(@PathVariable("id") Long id) throws Exception {
        return R.ok(pickingSaleService.view(id));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/finishProduction/{id}")
    @Log(title = "拣货完成", businessType = BusinessType.UPDATE)
    public R<Void> finishProduction(@PathVariable("id") Long id) throws Exception {
        return toAjax(pickingSaleService.finishProduction(id));
    }

    /**
     * 拣货撤销
     */
    @GetMapping("/repealProduction/{id}")
    @Log(title = "拣货撤销", businessType = BusinessType.UPDATE)
    public R<Void> repealProduction(@PathVariable("id") Long id) throws Exception {
        return toAjax(pickingSaleService.repealProduction(id));
    }

    /**
     * 出库交接
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:handover")
    @Log(title = "出库交接", businessType = BusinessType.UPDATE)
    @PostMapping("/overProduction")
    public R<?> overProduction(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return R.ok(pickingSaleService.overProduction(pickingRestDbVo));
    }


    /**
     * 销售发货出库
     */
    //@SaCheckPermission("warehouse:outbound:shipsale:outbound")
    @Log(title = "销售发货出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out")
    public R<?> out(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return R.ok(pickingSaleService.out(pickingRestDbVo));
    }

    /**
     * 销售发货列表（不分页）
     */
    @GetMapping("/noPageList")
    public List<PickingSaleDto> noPageList(PickingSaleVo pickingSaleVo) {
        //查询销售发货列表
        return pickingSaleService.noPageList(pickingSaleVo);
    }

    /**
     * 拣货提示
     */
    @GetMapping("/point/{id}")
    public R<?> point(@PathVariable("id") Long id) throws Exception {
        return R.ok(pickingSaleService.point(id));
    }

    /**
     * 销售发票通过单据号获取列表
     */
    @GetMapping("/saleInvoice")
    public TableDataInfo<SaleInvoiceDto> getSaleInvoice(SaleInvoiceVo saleInvoiceVo, PageQuery pageQuery) {
        return pickingSaleService.getSaleInvoice(saleInvoiceVo, pageQuery);
    }

    /**
     * 财务-销售发票新增单据列表
     */
    @GetMapping("/saleInvoicedList")
    public TableDataInfo<SaleInvoiceDto> saleInvoicedList(SaleInvoiceVo saleInvoiceVo, PageQuery pageQuery) {
        return pickingSaleService.saleInvoicedList(saleInvoiceVo, pageQuery);
    }

    /**
     * 查询收款单和付款单需要的销售发货和采购退货列表
     */
    @GetMapping("/getReceiptAndPaymentDoc")
    public TableDataInfo<ReceiptAndPaymentDocDto> getReceiptAndPaymentDoc(ReceiptAndPaymentDocVo saleServiceVo, PageQuery pageQuery) {
        return pickingSaleService.getReceiptAndPaymentDoc(saleServiceVo, pageQuery);
    }

    /**
     * 修改核单金额和开票数量
     */
    @PutMapping("/updateStorage")
    public boolean updateStorage(List<InvoiceVo> list) {
        return pickingSaleService.updateStorage(list);
    }

    /**
     * 通过客户编码获取总欠款数
     */
    @GetMapping("/getBalance")
    public String getBalance(String customerCode) {
        return pickingSaleService.getBalance(customerCode);
    }

    /**
     * 财务-核销单 通过客户编码获取销售发货数据
     */
    @GetMapping("/getDataByCode")
    public TableDataInfo<ImparityDto> getDataByCode(GetDataByCodeVo getDataByCodeVo, PageQuery pageQuery) {
        return pickingSaleService.getDataByCode(getDataByCodeVo, pageQuery);
    }

    /**
     * 财务-核销单 通过获取未全部核销的销售发货单、销售退货单、其他收入单数据
     */
    @GetMapping("/getNoVerificationList")
    public TableDataInfo<ImparityDto> getNoVerificationList(GetDataByCodeVo getDataByCodeVo, PageQuery pageQuery) {
        return pickingSaleService.getNoVerificationList(getDataByCodeVo, pageQuery);
    }
}
